package com.majx.controller;


import com.majx.pojo.Artifact;
import com.majx.service.ArtifactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/artifact")
public class ArtifactController {


    @Autowired
    @Qualifier("ArtifactService")
    private ArtifactService artifactService;

    /**
     * 查询分页文物
     * @param page
     * @return
     */
    @RequestMapping("/queryArtPage")
    @ResponseBody
    public List<Artifact> queryArtPage(@RequestParam("page") Integer page) {
        System.out.println("-----------------page="+page);
        int pageNow = page == null ? 1 : page;
        int pageSize = 5;
        int startRow = (pageNow - 1) * pageSize;

        System.out.println("startRow = "+startRow);
        System.out.println("-----------");
        System.out.println("artifactService = "+artifactService.queryArtPage(startRow));
        return artifactService.queryArtPage(startRow);
    }

    /**
     * 查询分页文物--带条件
     * @param page
     * @param artId
     * @param artName
     * @return
     */
    @RequestMapping("/selectArtPage")
    @ResponseBody
    public List<Artifact> selectArtPage(@RequestParam("page") Integer page,@RequestParam("artId") Integer artId,@RequestParam("artName") String artName) {
        int pageNow = page == null ? 1 : page;
        int pageSize = 5;
        int startRow = (pageNow - 1) * pageSize;

        return artifactService.selectArtPage(startRow,artId,artName);
    }

    /**
     * 添加文物
     * @param artifact
     * @return
     */
    @RequestMapping("/addArt")
    @ResponseBody
    public Integer addArt(@RequestBody Artifact artifact) {
        return artifactService.addArt(artifact);
    }

    /**
     * 删除文物
     * @param artId
     * @return
     */
    @RequestMapping("/deleteArt")
    @ResponseBody
    public Integer deleteArt(@RequestParam("artId") Integer artId) {
        return artifactService.deleteArt(artId);
    }


    /**
     * 批量删除文物
     * @param idList
     * @return
     */
    @RequestMapping("/deleteArtList")
    @ResponseBody
    public Integer deleteArtList(@RequestParam("idList") String idList) {

        /**
         * 因为传过来的参数多加了一个","，所以要做一些处理
         */
        idList.substring(0,idList.length() - 1);
        System.out.println("idList="+idList);

        List<Integer> idArray = new ArrayList<Integer>();
        for (String idStr : idList.split(",")) {
            Integer id = Integer.parseInt(idStr);
            idArray.add(id);
        }

        System.out.println("idArray="+idArray);
        for (Integer integer : idArray) {
            System.out.println("item = "+integer);
        }


        return artifactService.deleteArtList(idArray);
    }

    /**
     * 更新文物
     * @param artifact
     * @return
     */
    @RequestMapping("/updateArt")
    @ResponseBody
    public Integer updateArt(@RequestBody Artifact artifact){
        return artifactService.updateArt(artifact);
    }


    /**
     * 获取文物总数
     * @return
     */
    @RequestMapping("/getArtCount")
    @ResponseBody
    public Integer getArtCount(@RequestParam("artId") Integer artId,@RequestParam("artName") String artName) {
        return artifactService.getArtCount(artId,artName);
    }

}
